package com.password.desktop.UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.function.Consumer;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import com.password.desktop.util.SysUtil;

/**
 * 按钮组件，其借鉴了bootstrap3的按钮样式进行改造
 * 
 * @description
 * @author yzh
 * @date 2020年6月7日
 */
public class PMButton extends JLabel implements MouseListener {

	private static final long serialVersionUID = 1L;
	public static final ButtonStyle STYLE_NORMAL = new ButtonStyle(new Color(255, 255, 255), new Color(212, 212, 212),
			Color.GRAY, PMLineBorder.builder(1, new Color(204, 204, 204)),
			PMLineBorder.builder(1, new Color(140, 140, 140)));
	public static final ButtonStyle STYLE_BULE_BORDER = new ButtonStyle(Color.WHITE, new Color(234, 246, 255),
			Color.GRAY, PMLineBorder.builder(1, new Color(5, 186, 251)),
			PMLineBorder.builder(1, new Color(5, 186, 251)));
	public static final ButtonStyle STYLE_PRIMARY = new ButtonStyle(new Color(51, 122, 183), new Color(40, 96, 144),
			Color.white, PMLineBorder.builder(1, new Color(46, 109, 164)),
			PMLineBorder.builder(1, new Color(32, 77, 116)));
	public static final ButtonStyle STYLE_SUCCESS = new ButtonStyle(new Color(92, 184, 92), new Color(68, 157, 68),
			Color.white, PMLineBorder.builder(1, new Color(76, 174, 76)),
			PMLineBorder.builder(1, new Color(57, 132, 57)));
	public static final ButtonStyle STYLE_INFO = new ButtonStyle(new Color(91, 192, 222), new Color(38, 154, 188),
			Color.white, PMLineBorder.builder(1, new Color(70, 184, 218)),
			PMLineBorder.builder(1, new Color(27, 109, 133)));
	public static final ButtonStyle STYLE_WARN = new ButtonStyle(new Color(240, 173, 78), new Color(213, 133, 18),
			Color.white, PMLineBorder.builder(1, new Color(238, 162, 54)),
			PMLineBorder.builder(1, new Color(152, 95, 13)));
	public static final ButtonStyle STYLE_DANGER = new ButtonStyle(new Color(217, 83, 79), new Color(201, 48, 44),
			Color.white, PMLineBorder.builder(1, new Color(212, 63, 58)),
			PMLineBorder.builder(1, new Color(172, 41, 37)));

	private Consumer<PMButton> clickedEvent;
	private ButtonStyle style;

	public PMButton(String title, String tip, ButtonStyle style, Consumer<PMButton> clickedEvent) {
		super(title);
		this.style = style;
		init();
		this.clickedEvent = clickedEvent;
		this.setToolTipText(tip);
	}

	public PMButton(String title, ButtonStyle style, Consumer<PMButton> clickedEvent) {
		this(title, null, style, clickedEvent);
	}

	public PMButton(String title, Consumer<PMButton> clickedEvent) {
		this(title, STYLE_NORMAL, clickedEvent);
	}

	public void init() {
		setHorizontalAlignment(SwingConstants.CENTER);
		setForeground(style.foreground);
		setFont(SysUtil.FONT_DEFAULT_MIDDLE);
		setBackground(style.normalColor);
		setBorder(style.normalBorder);
		addMouseListener(this);
		setOpaque(true);
	}

	public void mousePressed(MouseEvent e) {// 点击后
		if (e.getSource() == this) {
			setFont(new Font("微软雅黑", 0, 14));
			if (e.getButton() == MouseEvent.BUTTON1)
				clickedEvent.accept(this);
		}
	}

	public void mouseReleased(MouseEvent e) {// 点击时
		if (e.getSource() == this) {
			setFont(new Font("微软雅黑", 0, 15));
		}
	}

	public void mouseEntered(MouseEvent e) {// 悬停
		if (e.getSource() == this) {
			setBackground(style.selectedStatusColor);
			setBorder(style.selectedStatusBorder);
		}
	}

	public void mouseExited(MouseEvent e) {// 悬停后
		if (e.getSource() == this) {
			setBackground(style.normalColor);
			setBorder(style.normalBorder);
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	public static class ButtonStyle {
		private Color normalColor;
		private Color selectedStatusColor;
		private Color foreground;
		private Border normalBorder;
		private Border selectedStatusBorder;

		public ButtonStyle(Color normalColor, Color selectedStatusColor) {
			this(normalColor, selectedStatusColor, Color.white, null, null);
		}

		public ButtonStyle(Color normalColor, Color selectedStatusColor, Color foreground) {
			this(normalColor, selectedStatusColor, foreground, null, null);
		}

		public ButtonStyle(Color normalColor, Color selectedStatusColor, Color foreground, Border normalBorder,
				Border selectedStatusBorder) {
			this.normalColor = normalColor;
			this.selectedStatusColor = selectedStatusColor;
			this.foreground = foreground;
			this.normalBorder = normalBorder;
			this.selectedStatusBorder = selectedStatusBorder;
		}

		public Border getNormalBorder() {
			return normalBorder;
		}

		public void setNormalBorder(Border normalBorder) {
			this.normalBorder = normalBorder;
		}

		public Border getSelectedStatusBorder() {
			return selectedStatusBorder;
		}

		public void setSelectedStatusBorder(Border selectedStatusBorder) {
			this.selectedStatusBorder = selectedStatusBorder;
		}

		public Color getForeground() {
			return foreground;
		}

		public void setForeground(Color foreground) {
			this.foreground = foreground;
		}

		public Color getNormalColor() {
			return normalColor;
		}

		public void setNormalColor(Color normalColor) {
			this.normalColor = normalColor;
		}

		public Color getSelectedStatusColor() {
			return selectedStatusColor;
		}

		public void setSelectedStatusColor(Color selectedStatusColor) {
			this.selectedStatusColor = selectedStatusColor;
		}

	}

}
